//商品详情展示
function showDetail(goodsId) {
    $.ajax({
        url : "goods/query/normal",
        async : true,
        type : "GET",
        data : {
            "goodsId" : goodsId
        },
        // 成功后开启模态框
        success : queryDetail,
        error : function(res) {
            alert("商品信息获取失败");
        },
        dataType : "json"
    });
}
function queryDetail(res) {
    if(res.code!=0){
        alert(res.msg);
    }
    var model = res.data;
    $("#d_goodsName").text(model.goodsName);
    $("#d_goodsParam").text(model.goodsParam);
    $("#d_goodsPic").attr('src',model.goodsPic);
    $("#d_goodsPrice").text(model.goodsPrice);
    var typeName;
    switch (model.goodsType) {
        case 1: typeName="食品/保健类"; break;
        case 2: typeName="服饰类"; break;
        case 3: typeName="皮具箱包类"; break;
        case 4: typeName="珠宝饰品类"; break;
        case 5: typeName="母婴/儿童类"; break;
        case 6: typeName="家电类"; break;
        case 7: typeName="数码类"; break;
        case 8: typeName="汽车配件类"; break;
        case 9: typeName="家具/建材类"; break;
        case 10: typeName="图书影音类"; break;
        default: typeName="不限";
    }
    $("#d_goodsType").text(typeName);
    $("#d_goodsStock").text(model.goodsStock);
    $("#d_goodsSales").text(model.goodsSales);
    $("#d_shopName").text(model.shopName);
    $("#d_goodsAddr").text(model.goodsAddr);
    $("#d_goodsTime").text(model.goodsTime);
    $("#d_modifyTime").text(model.modifyTime);

    if(model.isOnsale==0){
        $("#d_isOnsale").addClass("label-default");
    }else{
        $("#d_isOnsale").addClass("label-success");
    }
    if(model.isSeckill==0){
        $("#d_isSeckill").addClass("label-default");
    }else{
        $("#d_isSeckill").addClass("label-success");
    }
    if(model.isAuction==0){
        $("#d_isAuction").addClass("label-default");
    }else{
        $("#d_isAuction").addClass("label-success");
    }
    // 显示模态框
    $('#detailModal').modal('show');
}

//广告编辑展示
function showEdit(billboardId) {
    $.ajax({
        url : "billboard/query",
        async : true,
        type : "GET",
        data : {
            "billboardId" : billboardId
        },
        // 成功后开启模态框
        success : queryEdit,
        error : function(res) {
            alert("连接失败");
        },
        dataType : "json"
    });
}
function queryEdit(res) {
    if(res.code!=0){
        alert(res.msg);
    }
    var model = res.data;
    $("#e_billboardId").val(model.billboardId);
    $("#e_billboardTitle").val(model.billboardTitle);
    $("#e_billboardDesc").val(model.billboardDesc);
    $("#e_billboardUrl").val(model.billboardUrl);
    $("#e_billboardOrg").val(model.billboardOrg);

    // 显示模态框
    $('#editModal').modal('show');
}
//广告编辑提交
$("#editButton").click(function () {
    var data = new FormData(document.getElementById("editForm"));
    $.ajax({
        type: 'post',
        url: 'billboard/modify',
        dataType: 'json',
        data: data,
        contentType: false, //不设置内容类型
        processData: false, //不处理数据
        error: function(request) {
            alert("未知错误，请检查输入格式！");
        },
        success: function(res) {
            if(res.code==0) {
                alert("修改成功！");
                window.location.reload();
            }else{
                alert(res.msg);
            }
        }
    });
});

//广告撤下
function showUnder(billboardId) {
    if (confirm("确认撤下/投放该广告？")) {
        $.ajax({
            type: 'get',
            url: 'billboard/expire',
            dataType: 'json',
            data: {
                "billboardId": billboardId
            },
            contentType: false, //不设置内容类型
            error: function (request) {
                alert("未知错误，撤下/投放失败");
            },
            success: function (res) {
                if (res.code == 0) {
                    alert("撤下/投放成功！");
                    window.location.reload();
                } else {
                    alert(res.msg);
                }
            }
        });
    }
}


//广告置顶
function showTop(billboardId) {
    if (confirm("确认切换该广告置顶状态？")) {
        $.ajax({
            type: 'get',
            url: 'billboard/top',
            dataType: 'json',
            data: {
                "billboardId": billboardId
            },
            contentType: false, //不设置内容类型
            error: function (request) {
                alert("未知错误，切换失败");
            },
            success: function (res) {
                if (res.code == 0) {
                    alert("切换成功！");
                    window.location.reload();
                } else {
                    alert(res.msg);
                }
            }
        });
    }
}


//广告删除
function showDelete(billboardId) {
    if (confirm("确认删除该广告？")) {
        $.ajax({
            type: 'get',
            url: 'billboard/delete',
            dataType: 'json',
            data: {
                "billboardId": billboardId
            },
            contentType: false, //不设置内容类型
            error: function (request) {
                alert("未知错误，删除失败");
            },
            success: function (res) {
                if (res.code == 0) {
                    alert("删除广告成功！");
                    window.location.reload();
                } else {
                    alert(res.msg);
                }
            }
        });
    }
}

//广告添加展示
function showAdd() {
    // 显示模态框
    $('#addModal').modal('show');
}

//广告添加提交
$("#addButton").click(function () {
    var data = new FormData(document.getElementById("addForm"));
    //判断是否为空
    if(data.get('billboardTitle')===null||data.get('billboardTitle')===undefined||data.get('billboardTitle')==''||
        data.get('billboardDesc')===null||data.get('billboardDesc')===undefined||data.get('billboardDesc')==''||
        data.get('billboardUrl')===null||data.get('billboardUrl')===undefined||data.get('billboardUrl')==''||
        data.get('billboardOrg')===null||data.get('billboardOrg')===undefined||data.get('billboardOrg')==''||
        data.get('file')===null||data.get('file')===undefined||data.get('file')==''){
        alert("数据不能为空，请检查。");
        return 0;
    }
    $.ajax({
        type: 'post',
        url: 'billboard/add',
        dataType: 'json',
        data: data,
        contentType: false, //不设置内容类型
        processData: false, //不处理数据
        error: function(request) {
            alert("未知错误，请检查输入格式！");
        },
        success: function(res) {
            if(res.code==0) {
                alert("添加广告成功！");
                window.location.reload();
            }else{
                alert(res.msg);
            }
        }
    });
});
